package org.csu.laboratoryequipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.laboratoryequipment.common.CommonResponse;
import org.csu.laboratoryequipment.entity.Borrow;
import org.csu.laboratoryequipment.persistence.BorrowMapper;
import org.csu.laboratoryequipment.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("BorrowService")
public class BorrowServiceImpl implements BorrowService {

    @Autowired
    private BorrowMapper borrowMapper;

    @Override
    public CommonResponse<List<Borrow>> getBorrowList(){
        List<Borrow> borrowList = borrowMapper.selectList(null);
        List<Link> linkList = new ArrayList<>();

        Link link1 = new Link("http://localhost:8088/borrows","self");
        linkList.add(link1);
        for (int i = 0; i < borrowList.size(); i++){
            int id = borrowList.get(i).getTransactionID();

            Link link = new Link("http://localhost:8088/borrows/"+String.valueOf(id),"borrow");
            linkList.add(link);
        }
        return CommonResponse.createForSuccessMessage("SUCCESS", borrowList,linkList);
    }
    @Override
    public CommonResponse<List<Borrow>> getBorrowById(int transactionid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("TransactionID",transactionid);
        List<Borrow> borrowList = borrowMapper.selectList(queryWrapper);

        System.out.println(borrowList);


        List<Link> linkList = new ArrayList<>();
        for (int i = 0; i<borrowList.size(); i++){
            String id = borrowList.get(i).getUserID();
            Link link = new Link("http://localhost:8088/borrows/users/"+String.valueOf(id),"borrow");
            linkList.add(link);
        }
        return CommonResponse.createForSuccessMessage("SUCCESS",borrowList,linkList);
    }

    public CommonResponse<List<Borrow>> getBorrowByUserId(String userid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("UserID",userid);
        List<Borrow> borrowList = borrowMapper.selectList(queryWrapper);

        System.out.println(borrowList);


        List<Link> linkList = new ArrayList<>();
        for (int i = 0; i<borrowList.size(); i++){
            int id = borrowList.get(i).getTransactionID();
            Link link = new Link("http://localhost:8088/borrows/"+String.valueOf(id),"borrow");
            linkList.add(link);
        }
        return CommonResponse.createForSuccessMessage("SUCCESS",borrowList,linkList);
    }



    @Override
    public CommonResponse<Borrow> deleteBorrowById(int transactionid){
        Borrow borrow = borrowMapper.selectById(transactionid);
        borrowMapper.deleteById(transactionid);
        List<Link> linkList = new ArrayList<>();
        Link link = new Link("http://localhost:8088/borrows/"+String.valueOf(transactionid),"delete");
        linkList.add(link);
        return CommonResponse.createForSuccessMessage("删除成功", borrow,linkList);
    }


    @Override
    public CommonResponse<Borrow> postBorrow(String userid, String equipmentid, Timestamp borrowdate){
        Borrow borrow = new Borrow();
        borrow.setUserID(userid);
        borrow.setEquipmentID(equipmentid);
        borrow.setBorrowDate(borrowdate);
        borrow.setStatus(0);
        borrowMapper.insert(borrow);
        int transactionid = borrow.getTransactionID();
        List<Link> linkList = new ArrayList<>();
        Link link = new Link("http://localhost:8088/borrows/"+String.valueOf(transactionid),"post");
        linkList.add(link);
        return CommonResponse.createForSuccessMessage("新增成功", borrow,linkList);
    }


    @Override
    public CommonResponse<Borrow> putBorrow(int transactionid, Timestamp returndate){
        Borrow borrow = new Borrow();
        borrow.setTransactionID(transactionid);
        borrow.setReturnDate(returndate);
        borrow.setStatus(1);
        borrowMapper.updateById(borrow);
        List<Link> linkList = new ArrayList<>();
        Link link = new Link("http://localhost:8088/borrows/"+String.valueOf(transactionid),"put");
        linkList.add(link);
        return CommonResponse.createForSuccessMessage("更新成功", borrow,linkList);
    }


}
